<?php
	class khachhangdb
	{
		public function Them($MaKH = "", 
							 $Ho = "", 
							 $Ten = "",
							 $GioiTinh = 0,
							 $NgaySinh = "",
							 $DiaChi = "", 
				 			 $SoCMND = "", 
							 $NgayCap = "", 
							 $NoiCap = "",
							 $NgayDangKy = "",
							 $DienThoai = "", 
							 $Email = "", 
							 $MaDiaBan = "", 
							 $MatKhau = "",
							 $Khoa = 0)
		{
			$sql = "INSERT INTO khachhang(MaKH, Ho, Ten, GioiTinh, NgaySinh, DiaChi, SoCMND, NgayCap, NoiCap, NgayDangKy, DienThoai, Email, MaDiaBan, MatKhau, Khoa)
					VALUES('$MaKH', 
							'$Ho', 
							'$Ten', 
							$GioiTinh, 
							'$NgaySinh', 
							'$DiaChi', 
							'$SoCMND', 
							'$NgayCap', 
							'$NoiCap', 
							'$NgayDangKy',
							'$DienThoai',
							'$Email', 
							'$MaDiaBan',
							'$MatKhau', 
							$Khoa)";				
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function CapNhat($MaKH = "", 
							 	$Ho = "", 
							 	$Ten = "",
							 	$GioiTinh = 0,
								$NgaySinh = "",
							 	$DiaChi = "", 
				 			 	$SoCMND = "", 
							 	$NgayCap = "", 
							 	$NoiCap = "", 
							 	$DienThoai = "", 
							 	$Email = "", 
							 	$MaDiaBan = "", 
							 	$MatKhau = "")
		{
			$sql = "UPDATE khachhang SET 
			Ho = '$Ho',
			Ten = '$Ten',
			GioiTinh = $GioiTinh,
			NgaySinh = '$NgaySinh',
			DiaChi = '$DiaChi',
			SoCMND = '$SoCMND',
			NgayCap = '$NgayCap',
			NoiCap = '$NoiCap',
			DienThoai = '$DienThoai',
			Email = '$Email'";
			
			if($MaDiaBan != "")
				$sql .= ", MaDiaBan = '$MaDiaBan'";
			
			if($MatKhau != "")
				$sql .= ", MatKhau = '$MatKhau'";
			
			$sql .= " WHERE MaKH = '$MaKH'";
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}

		public function LayDanhSach($MaKH = "", 
							 		$Ho = "", 
							 		$Ten = "",
							 		$GioiTinh = 0,
									$NgaySinh = "",
									$SoCMND = "",
									$NgayThamGia = "",
									$DienThoai = "",
									$Email = "",
							 		$MaDiaBan = "", 
							 		$MatKhau = "",
									$Khoa = 0,
									$TimDiaBan = "")
		{
			$sql = "SELECT * 
					FROM khachhang
					WHERE (1)";
			
			if($MaKH != "")
				$sql .= " AND MaKH = '$MaKH'";
				
			if($Ho != "")
				$sql .= " AND Ho = '$Ho'";
			
			if($Ten != "")
				$sql .= " AND Ten = '$Ten'";

			if($GioiTinh > 0)
				$sql .= " AND GioiTinh = $GioiTinh";
			
			if($NgaySinh != "")
				$sql .= " AND NgaySinh = '$NgaySinh'";
			
			if($SoCMND != "")
				$sql .= " AND SoCMND = '$SoCMND'";
			
			if($NgayDangKy != "")
				$sql .= " AND NgayDangKy = '$NgayDangKy'";
			
			if($DienThoai != "")
				$sql .= " AND DienThoai = '$DienThoai'";
			
			if($Email != "")
				$sql .= " AND Email = '$Email'";
			
			if($MaDiaBan != "")
				$sql .= " AND MaKH like '%$MaDiaBan%'";
			
			if($MatKhau != "")
				$sql .= " AND MatKhau = '$MatKhau'";
			
			if($Khoa > 0)
				$sql .= " AND Khoa = $Khoa";
			
			if($TimDiaBan != "")
				$sql .= " AND MaKH like '%$TimDiaBan%'";
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		public function CapNhatKhoa($MaKH = "",
									$Khoa = 0)
		{
			$sql = "update khachhang set Khoa = ".dataservice::QuoteSmart($Khoa)."
					where MaKH = ".dataservice::QuoteSmart($MaKH);
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function CapNhatMatKhau($MaKH = "", $MatKhauMoi="")
		{
			$sql = "update khachhang set MatKhau = '".$MatKhauMoi."'
					where MaKH = ".dataservice::QuoteSmart($MaKH);
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function TonTaiTrongSoTietKiem($MaKH = "")
		{
			$sql = "SELECT * FROM sotietkiem WHERE 1";
			if($MaKH != "")
				$sql.= " AND MaKH = '$MaKH'";
		
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		public function Xoa($MaKH = "")
		{
			$sql = "DELETE 
					FROM `khachhang` 
					WHERE MaKH = '$MaKH'";
			
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function XoaTatCa()
		{
			$sql = "DELETE FROM khachhang";			
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
	}
?>